package com.fin.zw.aiqas.util;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateUtil {

    /**
     * 获取当前日期
     *
     * @return
     */
    public static LocalDate getNowLocalDate() {
        return LocalDate.now();
    }

    public static Date getNowDate() {
        LocalDate localDate = getNowLocalDate();
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }
    /**
     * 获取当前时间
     *
     * @return
     */
    public static LocalDateTime getNowLocalDateTime() {
        return LocalDateTime.now();
    }

    public static String dateToString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    /**
     * 获取过去一月时间
     *
     * @return
     */
    public static Date getBeforeMonth() {
        LocalDate date = LocalDate.now();
        date = date.minusMonths(1);
        ZonedDateTime zonedDateTime = date.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 计算两个日期之间相差多少天
     *
     * @param start
     * @param end
     * @return
     */
    public static long minusLocalDate(Date start, Date end) {
        LocalDate startDate = dateToLocalDate(start);
        LocalDate endDate = dateToLocalDate(end);
        long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
        return daysDiff;
    }

    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }


    public static LocalDate addDays(Date date, Integer day) {
        LocalDate localDate = dateToLocalDate(date);
        LocalDate newDate = localDate.plus(day, ChronoUnit.DAYS);
        return newDate;
    }

    /**
     * 毫秒转化 LocalDateTime
     *
     * @param time
     * @return
     */
    public static LocalDateTime longToLocalDateTime(long time) {
        Date date = new Date(time);
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 比较两个 LocalDateTime 相差多少秒
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long compare(LocalDateTime time1, LocalDateTime time2) {
        long between = ChronoUnit.SECONDS.between(time2, time1);
        return between;
    }
}
